<template>
  <div class="announcement-detail">
    <div class="form-wrap">
      <el-form label-position="top" label-width="80px" :model="formData">
        <el-form-item :label="$t('configLazyLoad.process.nodeName')">
          <el-input v-model.trim="formData.DisplayName"></el-input>
        </el-form-item>
        <el-form-item label="Url：">
          <el-input v-model="formData.Url"></el-input>
        </el-form-item>
        <el-form-item label="MD5加密值：">
          <el-input v-model="formData.MD5"></el-input>
        </el-form-item>
        <el-form-item label="提示语音：">
          <el-select v-model="formData.Filename" placeholder="请选择" style="width: 100%;">
            <el-option
              label="请选择"
              value="">
            </el-option>
            <el-option
              v-for="item in ivr_sound.ivr_sound"
              v-if="item.SoundType == 'normal'"
              :label="item.DisplayName"
              :value="item.Filename">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="超时时间：">
          <el-input v-model="formData.Timeout" type="number" min="0">
            <template slot="append">秒</template>
          </el-input>
        </el-form-item>
        <el-form-item label="返回结果赋值变量：">
          <el-select v-model="formData.Variable" placeholder="请选择" style="width: 100%;">
            <el-option
              v-for="item in vars.vars"
              :label="item.VarName"
              :value="item.VarName">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="成功转向：">
          <node-select-item ref="nodeSelect" :curPbx="curPbx" :ToPeerType="form.NextPeerType" :ToPeer="form.NextPeer"></node-select-item>
        </el-form-item>
        <el-form-item label="超时转向：">
          <node-select-item ref="nodeSelectTimeout" :curPbx="curPbx" :ToPeerType="form.ToPeerTypeWhenTimeout" :ToPeer="form.ToPeerWhenTimeout"></node-select-item>
        </el-form-item>
      </el-form>
    </div>
    <div class="footer">
      <el-button class='slide-box__button' @click='cancelClick'>
        取消
      </el-button>
      <el-button class='slide-box__button' type="primary" @click='saveClick'>
        保存
      </el-button>
    </div>
  </div>
</template>
<script>
  import {deepClone} from '@/utils/m7Utils'
  import nodeSelectItem from '../../base/nodeSelectItem.vue'
  export default {
    name: 'mainIvrValidationNodeLink',
    data () {
      return {
        formData: {}
      }
    },
    props: {
      form: {
        type: Object,
        default: function () {
          return {}
        }
      }
    },
    components: {
      nodeSelectItem
    },
    computed: {
      Account () {
        return this.$store.state.session.account.account
      },
      curPbx () {
        return this.$store.state.config.callCenter.currentPBX
      },
      ivr_sound () {
        return this.$store.state.session.ivrMap[this.curPbx].data
      },
      vars () {
        return this.$store.state.session.ivrMap[this.curPbx].data
      }
    },
    methods: {
      init () {
        this.formData = deepClone(this.form)
        let flag = false
        if (this.ivr_sound.ivr_sound && this.ivr_sound.ivr_sound.length > 0) {
          this.ivr_sound.ivr_sound.forEach(item => {
            if (this.form.Filename === item.Filename) {
              flag = true
            }
          })
        }
        if (!flag) {
          this.formData.Filename = ''
        } else {
          this.formData.Filename = this.form.Filename
        }
      },
      cancelClick () {
        this.$emit('cancelClick')
      },
      saveClick () {
        if (!this.formData.DisplayName) {
          this.$message({
            type: 'error',
            message: '节点名称不能为空!'
          })
          return
        }
        if (!this.formData.Url) {
          this.$message({
            type: 'error',
            message: 'url地址不能为空!'
          })
          return
        }
        if (!this.formData.Timeout) {
          this.$message({
            type: 'error',
            message: '超时时间不能为空!'
          })
          return
        }
        this.formData.NextPeerType = this.$refs.nodeSelect.ToPeerTypeThis
        this.formData.NextPeer = this.$refs.nodeSelect.ToPeerThis
        this.formData.ToPeerTypeWhenTimeout = this.$refs.nodeSelectTimeout.ToPeerTypeThis
        this.formData.ToPeerWhenTimeout = this.$refs.nodeSelectTimeout.ToPeerThis
        let obj = deepClone(this.formData)
        obj.PBX = this.curPbx
        obj.Account = this.Account
        this.$store.dispatch('updateCallUrl', obj).then(res => {
          if (res && res.success) {
            this.$message({
              type: 'success',
              message: '保存成功!'
            })
            this.updateVuexPbx()
          }
        })
      },
      updateVuexPbx () {
        this.$store.dispatch('queryCallUrl', {Account: this.Account, PBX: this.curPbx}).then(resp => {
          if (resp && resp.success) {
            this.$store.commit('SET_IVRMAP_DETAIL', {PBX: this.curPbx, typeName: 'ivr_node_call_url', data: resp.list || [], flag: 'data', node: this})
          }
          this.$emit('saveClick')
        })
      }
    },
    watch: {
      form: {
        deep: true,
        handler (nv, ov) {
          this.init()
          this.$refs.nodeSelect.init()
          this.$refs.nodeSelectTimeout.init()
        }
      }
    },
    beforeMount () {
      this.init()
    }
  }
</script>

<style lang="stylus" scoped>
  .announcement-detail
    position relative
    width 100%
    height 100%
  .form-wrap
    padding 20px
    height calc(100% - 100px)
    overflow-y scroll
  .footer
    position absolute
    bottom 0
    left 0
    width 100%
    background rgba(255,255,255,1)
    box-shadow 0px -2px 4px 0px rgba(5,49,38,0.05)
    border-top 1px solid rgba(232,232,232,1)
    display flex
    justify-content center
    padding 13px 0
    box-sizing border-box
    .slide-box__button
      padding 8px 20px 8px
      box-sizing border-box
</style>
